package com.xzq.wanandroid.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xzq.wanandroid.R;
import com.xzq.wanandroid.ui.fragment.HomeFragment;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.container)
    FrameLayout container;
    @BindView(R.id.navigation_bottom)
    BottomNavigationView mBottomNavigationView;

    private Context context;
    private Unbinder unbinder;
    private static final int INDEX_HOMEPAGE = 0;
    private static final int INDEX_PROJECT = 1;
    private static final int INDEX_SQUARE = 2;
    private static final int INDEX_WECHAT = 3;
    private static final int INDEX_ME = 4;
    private int mLastIndex = -1;
    private Fragment mCurrentFragment;
    private Fragment mLastFragment;
    private SparseArray<Fragment> fragmentSparseArray = new SparseArray<>();

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        EventBus.getDefault().register(this);
        context = getApplicationContext();
        unbinder = ButterKnife.bind(this);
        initBottomNavigation();


    }

    private void initBottomNavigation() {
        mBottomNavigationView.setOnNavigationItemSelectedListener(
                MenuItem -> {
                    switch (MenuItem.getItemId()) {
                        case R.id.menu_home:
                            switchFragment(INDEX_HOMEPAGE);
                            return true;
                        case R.id.menu_project:
                            switchFragment(INDEX_PROJECT);
                            return true;
                        case R.id.menu_square:
                            switchFragment(INDEX_SQUARE);
                            return true;
                        case R.id.menu_wechat:
                            switchFragment(INDEX_WECHAT);
                            return true;
                        case R.id.menu_me:
                            switchFragment(INDEX_ME);
                            return true;
                        default:
                            return false;
                    }
                });
    }


    private void switchFragment(int index) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //给当前的fragment和上一个fragment都加上TAG，便于添加时候处理，免于出现添加混乱
        mCurrentFragment = fragmentManager.findFragmentByTag("fragment" + index);
        mLastFragment = fragmentManager.findFragmentByTag("fragment" + mLastIndex);
        //如果位置不同
        if (index != mLastIndex) {
            if (mLastFragment != null) {
                //将上一个fragment隐藏
                transaction.hide(mLastFragment);
            }
            if (mCurrentFragment == null) {
                mCurrentFragment = getFragment(index);
                transaction.add(R.id.container, mCurrentFragment, "fragment" + index);
                transaction.commit();
            } else {
                transaction.show(mCurrentFragment);
            }

        }
        //如果位置相同或者打开一个新的应用
        if (index == mLastIndex) {
            if (mCurrentFragment == null) {
                mCurrentFragment = getFragment(index);
                transaction.add(R.id.container, mCurrentFragment, "fragment" + index);
            }
        }
        transaction.commit();
        mLastIndex = index;
    }

    private Fragment getFragment(int index) {
        Fragment fragment = fragmentSparseArray.get(index);
        if (fragment==null){
            switch (index){
                case INDEX_HOMEPAGE:
                    fragment= HomeFragment.getInstance();
                    break;
                case INDEX_PROJECT:
                    break;
                case INDEX_SQUARE:
                    break;
                case INDEX_WECHAT:
                    break;
                case INDEX_ME:
                    break;
                default:
                    break;
            }
        }
        return fragment;
    }

}